//
//  UserInfoTitleView.m
//  JiaoX
//
//  Created by yinqy on 15/4/8.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "UserInfo.h"
#import "UserInfoTitleView.h"
#import "JXWebViewController.h"
#import "JXChatCtl.h"

@interface UserInfoTitleView()
{
    UIImageView *_headImg;
    UILabel *_nameLabel;
    UIImageView *_genderImg;
    UIImageView *_arrowImg;
    UIButton *_button;
    
    NSString *_url;
    
    UserInfo *_userInfo;
}

@end
@implementation UserInfoTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setWidth:JXScreenW];
        
        UIImageView *headImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, SCALEX(39), SCALEX(39))];
        [headImg.layer setCornerRadius:headImg.height/2];
        [headImg.layer setMasksToBounds:YES];
        [self addSubview:headImg];
        _headImg = headImg;
        
        _nameLabel = [[UILabel alloc]init];
        [_nameLabel setFont:SYS_FONT(SCALEX(14))];
        [_nameLabel setTextColor:JXColor(102, 102, 102)];
        [self addSubview:_nameLabel];
        
        _genderImg = [[UIImageView alloc]init];
        [self addSubview:_genderImg];
        
        _arrowImg = [[UIImageView alloc]initWithImage:IMG(@"self_item_arrow")];
        [_arrowImg setX:self.width-_arrowImg.width-10];
        [_arrowImg setCenterY:_headImg.centerY];
        [self addSubview:_arrowImg];
        
        _button = [[UIButton alloc]init];
        [_button setSize:CGSizeMake(SCALEX(66), SCALEX(26))];
        [_button setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [_button setTitle:@"私信" forState:UIControlStateNormal];
        [_button setImage:IMG(@"ic_pop_chat") forState:UIControlStateNormal];
        [_button setCenterY:_headImg.centerY];
        [_button.titleLabel setFont:SYS_FONT(SCALEX(12))];
        [_button setBackgroundColor:JXThemeColor];
        [_button setX:self.width-10-_button.width];
        [_button setHidden:YES];
        [self addSubview:_button];
        [self setHeight:CGRectGetMaxY(_headImg.frame)+10];
        
    }
    return self;
}

-(void)setUserInfo:(UserInfo *)userInfo{
    _userInfo = userInfo;
    [_headImg sd_setImageWithURL:[NSURL URLWithString:userInfo.userImg]];
    [_nameLabel setText:userInfo.nickName];
    [_nameLabel setSize:[_nameLabel.text sizeWithFont:_nameLabel.font]];
    [_nameLabel setX:CGRectGetMaxX(_headImg.frame)+10];
    [_nameLabel setCenterY:_headImg.centerY];
    
    //默认为未知性别
    NSString *genderImg = @"ic_gender_unknow";
    //判断是男是女
    if ([[NSString stringWithFormat:@"%@",userInfo.userGender] isEqualToString:@"1"]) {
        genderImg = @"ic_gender_man";
    }else if([[NSString stringWithFormat:@"%@",userInfo.userGender] isEqualToString:@"2"]){
        genderImg = @"ic_gender_woman";
    }
    //判断是否是vip
    if (userInfo.level) {
        genderImg = [NSString stringWithFormat:@"%@_vip",genderImg];
    }
    [_genderImg setImage:[UIImage imageNamed:genderImg]];
    [_genderImg setSize:_genderImg.image.size];
    [_genderImg setCenterY:_nameLabel.centerY];
    [_genderImg setX:CGRectGetMaxX(_nameLabel.frame)+5];
}

-(void)setType:(int)type{
    _type = type;
    if (!type) {
        //代表自己
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        [self addGestureRecognizer:tap];
    }else{
        [_arrowImg setHidden:YES];
        [_button setHidden:NO];
        [_button addTarget:self action:@selector(toChat:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(void)setUrl:(NSString *)url{
    [self setType:0];
    _url = url;
}

-(void)click:(UITapGestureRecognizer *)ges{
    JXWebViewController *ctl = [JXWebViewController initWithUrl:_url title:@"玩转论坛"];
    [[self.superview viewController].navigationController pushViewController:ctl animated:YES];
}

-(void)toChat:(UIButton *)btn{
    JXChatCtl *ctl = [[JXChatCtl alloc]initWithUserInfo:_userInfo];
    [[self.superview viewController].navigationController pushViewController:ctl animated:YES];
}

@end
